<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
package <%= packageName %>.web.rest;
<%_ if (authenticationType === 'oauth2') { _%>

<%_ if (databaseType === 'cassandra') { _%>
import <%= packageName %>.AbstractCassandraTest;
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
import <%= packageName %>.AbstractNeo4jIT;
<%_ } _%>
<%_ if (cacheProvider === 'redis') { _%>
import <%= packageName %>.RedisTestContainerExtension;
<%_ } _%>
<%_ if (reactiveSqlTestContainers) { _%>
import <%= packageName %>.ReactiveSqlTestContainerExtension;
<%_ } _%>
import <%= packageName %>.<%= mainClass %>;
import <%= packageName %>.config.TestSecurityConfiguration;
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.service.UserService;

<%_ if (reactive) { _%>
import org.junit.jupiter.api.BeforeEach;
<%_ } _%>
import org.junit.jupiter.api.Test;
<%_ if (databaseType === 'neo4j' || cacheProvider === 'redis' || (reactive && ['mysql', 'postgresql', 'mssql'].includes(prodDatabaseType))) { _%>
import org.junit.jupiter.api.extension.ExtendWith;
<%_ } _%>
import org.springframework.beans.factory.annotation.Autowired;
<%_ if (!reactive) { _%>
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
<%_ } else { _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
<%_ } _%>
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
<%_ if (!reactive) { _%>
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.oauth2.client.authentication.OAuth2AuthenticationToken;
import org.springframework.security.oauth2.core.user.DefaultOAuth2User;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.security.test.context.TestSecurityContextHolder;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.web.servlet.MockMvc;
    <%_ if (databaseType === 'sql' && !reactive) { _%>
import org.springframework.transaction.annotation.Transactional;
    <%_ } _%>

import java.util.Collection;
<%_ } else { _%>
import org.springframework.security.oauth2.core.oidc.OidcIdToken;
import org.springframework.test.web.reactive.server.WebTestClient;
    <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
import reactor.core.publisher.Mono;
    <%_ } _%>

import java.time.Instant;
<%_ } _%>
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import static org.mockito.Mockito.*;
<%_ } _%>
import static <%= packageName %>.web.rest.AccountResourceIT.TEST_USER_LOGIN;
import org.springframework.security.test.context.support.WithMockUser;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ if (reactive) { _%>
import static <%= packageName %>.web.rest.TestUtil.ID_TOKEN;
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.*;
<%_ } _%>

/**
 * Integration tests for the {@link AccountResource} REST controller.
 */
<%_ if (reactiveSqlTestContainers) { _%>
@ExtendWith(ReactiveSqlTestContainerExtension.class)
<%_ } _%>  
<%_ if (!reactive) { _%>
@AutoConfigureMockMvc
<%_ } else { _%>
@AutoConfigureWebTestClient
<%_ } _%>
@WithMockUser(value = TEST_USER_LOGIN)
@SpringBootTest(classes = {<%= mainClass %>.class, TestSecurityConfiguration.class})
<%_ if (cacheProvider === 'redis') { _%>
@ExtendWith(RedisTestContainerExtension.class)
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
@ExtendWith(AbstractNeo4jIT.class)
<%_ } _%>
public class AccountResourceIT <% if (databaseType === 'cassandra') { %>extends AbstractCassandraTest <% } %>{

    static final String TEST_USER_LOGIN = "test";

    <%_ if (!reactive) { _%>
    @Autowired
    private MockMvc restAccountMockMvc;
    <%_ } else { _%>
    private OidcIdToken idToken;

    @Autowired
    private WebTestClient webTestClient;
    <%_ } _%>

    <%_ if (reactive) { _%>
    @BeforeEach
    public void setup() {
        Map<String, Object> claims = new HashMap<>();
        claims.put("groups", Collections.singletonList(AuthoritiesConstants.ADMIN));
        claims.put("sub", "jane");
        claims.put("email", "jane.doe@jhipster.com");
        this.idToken = new OidcIdToken(ID_TOKEN, Instant.now(),
            Instant.now().plusSeconds(60), claims);
    }

    <%_ } _%>
    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    public void testGetExistingAccount() <% if (!reactive) { %>throws Exception <% } %>{
    <%_ if (!reactive) { _%>
        Map<String, Object> userDetails = new HashMap<>();
        userDetails.put("sub", TEST_USER_LOGIN);
        userDetails.put("email", "john.doe@jhipster.com");
        Collection<GrantedAuthority> authorities = Collections.singletonList(new SimpleGrantedAuthority(AuthoritiesConstants.ADMIN));
        OAuth2User user = new DefaultOAuth2User(authorities, userDetails, "sub");
        OAuth2AuthenticationToken authentication = new OAuth2AuthenticationToken(user, authorities, "oidc");
        TestSecurityContextHolder.getContext().setAuthentication(authentication);

        restAccountMockMvc.perform(get("/api/account")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.login").value(TEST_USER_LOGIN))
            .andExpect(jsonPath("$.email").value("john.doe@jhipster.com"))
            .andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
    <%_ } else { _%>
        webTestClient
            .mutateWith(mockAuthentication(TestUtil.authenticationToken(idToken)))
            .mutateWith(csrf())
            .get().uri("/api/account")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
            .expectBody()
            .jsonPath("$.login").isEqualTo("jane")
            .jsonPath("$.email").isEqualTo("jane.doe@jhipster.com")
            .jsonPath("$.authorities").isEqualTo(AuthoritiesConstants.ADMIN);
    <%_ } _%>
    }

    @Test
    public void testGetUnknownAccount() <% if (!reactive) { %>throws Exception <% } %>{
    <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(get("/api/account")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isInternalServerError());
    <%_ } else { _%>
        webTestClient.get().uri("/api/account")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().is5xxServerError();
    <%_ } _%>
    }
}
<%_ } else if (skipUserManagement) { // not oauth, skipUserManagement _%>

<%_ if (databaseType === 'cassandra') { _%>
import <%= packageName %>.AbstractCassandraTest;
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
import <%= packageName %>.AbstractNeo4jIT;
<%_ } _%>
<%_ if (cacheProvider === 'redis') { _%>
import <%= packageName %>.RedisTestContainerExtension;
<%_ } _%>
import <%= packageName %>.<%= mainClass %>;
<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import <%= packageName %>.repository.search.UserSearchRepository;
<%_ } _%>
import <%= packageName %>.security.AuthoritiesConstants;
import org.junit.jupiter.api.Test;
<%_ if (databaseType === 'neo4j' || cacheProvider === 'redis' || (reactive && ['mysql', 'postgresql', 'mssql'].includes(prodDatabaseType))) { _%>
import org.junit.jupiter.api.extension.ExtendWith;
<%_ } _%>
<%_ if (reactiveSqlTestContainers) { _%>
import <%= packageName %>.ReactiveSqlTestContainerExtension;
<%_ } _%>
import org.springframework.beans.factory.annotation.Autowired;
<%_ if (!reactive) { _%>
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
<%_ } else { _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
<%_ } _%>
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
<%_ if (!reactive) { _%>
import org.springframework.test.web.servlet.MockMvc;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ } else { _%>
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ } _%>
<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import reactor.core.publisher.Mono;

import static org.mockito.Mockito.*;
<%_ } _%>
import static <%= packageName %>.web.rest.AccountResourceIT.TEST_USER_LOGIN;

/**
 * Integration tests for the {@link AccountResource} REST controller.
 */
<%_ if (reactiveSqlTestContainers) { _%>
@ExtendWith(ReactiveSqlTestContainerExtension.class)
<%_ } _%>  
<%_ if (!reactive) { _%>
@AutoConfigureMockMvc
<%_ } else { _%>
@AutoConfigureWebTestClient
<%_ } _%>
@WithMockUser(value = TEST_USER_LOGIN)
@SpringBootTest(classes = <%= mainClass %>.class)
<%_ if (cacheProvider === 'redis') { _%>
@ExtendWith(RedisTestContainerExtension.class)
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
@ExtendWith(AbstractNeo4jIT.class)
<%_ } _%>
public class AccountResourceIT <% if (databaseType === 'cassandra') { %>extends AbstractCassandraTest <% } %>{
    static final String TEST_USER_LOGIN = "test";

    @Autowired
    <%_ if (!reactive) { _%>
    private MockMvc restAccountMockMvc;
    <%_ } else { _%>
    private WebTestClient accountWebTestClient;
    <%_ } _%>
    <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>

    @Autowired
    private UserSearchRepository mockUserSearchRepository;
    <%_ } _%>

    @Test
    @WithMockUser(username = TEST_USER_LOGIN, authorities = AuthoritiesConstants.ADMIN)
    public void testGetExistingAccount() <% if (!reactive) { %>throws Exception <% } %>{
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(get("/api/account")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.login").value(TEST_USER_LOGIN))
            .andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
        <%_ } else { _%>
        accountWebTestClient.get().uri("/api/account")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON)
            .expectBody()
            .jsonPath("$.login").isEqualTo(TEST_USER_LOGIN)
            .jsonPath("$.authorities").isEqualTo(AuthoritiesConstants.ADMIN);
        <%_ } _%>
    }
}
<%_ } else {  // not oauth, not skipUserManagement _%>

<%_ if (databaseType === 'cassandra') { _%>
import <%= packageName %>.AbstractCassandraTest;
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
import <%= packageName %>.AbstractNeo4jIT;
<%_ } _%>
<%_ if (cacheProvider === 'redis') { _%>
import <%= packageName %>.RedisTestContainerExtension;
<%_ } _%>
<%_ if (reactiveSqlTestContainers) { _%>
import <%= packageName %>.ReactiveSqlTestContainerExtension;
<%_ } _%>
import <%= packageName %>.<%= mainClass %>;
import <%= packageName %>.config.Constants;
<%_ if (authenticationType === 'session' && !reactive) { _%>
import <%= packageName %>.domain.PersistentToken;
<%_ } _%>
import <%= packageName %>.domain.<%= asEntity('User') %>;
<%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j' || databaseType === 'couchbase') { _%>
import <%= packageName %>.repository.AuthorityRepository;
<%_ } _%>
<%_ if (authenticationType === 'session' && !reactive) { _%>
import <%= packageName %>.repository.PersistentTokenRepository;
<%_ } _%>
import <%= packageName %>.repository.UserRepository;
<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import <%= packageName %>.repository.search.UserSearchRepository;
<%_ } _%>
import <%= packageName %>.security.AuthoritiesConstants;
import <%= packageName %>.service.UserService;
import <%= packageName %>.service.dto.PasswordChangeDTO;
import <%= packageName %>.service.dto.<%= asDto('User') %>;
import <%= packageName %>.web.rest.vm.KeyAndPasswordVM;
import <%= packageName %>.web.rest.vm.ManagedUserVM;
import org.apache.commons.lang3.RandomStringUtils;
<%_ if (databaseType === 'neo4j' || cacheProvider === 'redis' || reactiveSqlTestContainers) { _%>
import org.junit.jupiter.api.extension.ExtendWith;
<%_ } _%>
<%_ if (reactive && testsNeedCsrf || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>
import org.junit.jupiter.api.BeforeEach;
<%_ } _%>
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
<%_ if (!reactive) { _%>
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
<%_ } else { _%>
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient;
<%_ } _%>
import org.springframework.boot.test.context.SpringBootTest;
<%_ if (reactive) { _%>
import org.springframework.http.HttpStatus;
<%_ } _%>
import org.springframework.http.MediaType;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.test.context.support.WithMockUser;
<%_ if (reactive) { _%>
import org.springframework.test.web.reactive.server.WebTestClient;
<%_ } _%>
<%_ if (!reactive) { _%>
import org.springframework.test.web.servlet.MockMvc;
<%_ } _%>
<%_ if (databaseType === 'sql' && !reactive) { _%>
import org.springframework.transaction.annotation.Transactional;
<%_ } _%>
<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import reactor.core.publisher.Mono;
<%_ } _%>

import java.time.Instant;
<%_ if (authenticationType === 'session' && !reactive && ['sql', 'mongodb', 'neo4j', 'couchbase'].includes(databaseType)) { _%>
import java.time.LocalDate;
<%_ } _%>
import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
<%_ if (reactive && searchEngine === 'elasticsearch') { _%>
import static org.mockito.Mockito.*;
<%_ } _%>
<%_ if (authenticationType === 'session' && !reactive) { _%>
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.hasItem;
<%_ } _%>
import static <%= packageName %>.web.rest.AccountResourceIT.TEST_USER_LOGIN;
<%_ if (!reactive) { _%>
    <%_ if (testsNeedCsrf) { _%>
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf;
    <%_ } _%>
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
<%_ } else if (testsNeedCsrf) { _%>
import static org.springframework.security.test.web.reactive.server.SecurityMockServerConfigurers.csrf;
<%_ } _%>

/**
 * Integration tests for the {@link AccountResource} REST controller.
 */
<%_ if (reactiveSqlTestContainers) { _%>
@ExtendWith(ReactiveSqlTestContainerExtension.class)
<%_ } _%>  
<%_ if (!reactive) { _%>
@AutoConfigureMockMvc
<%_ } else { _%>
@AutoConfigureWebTestClient
<%_ } _%>
@WithMockUser(value = TEST_USER_LOGIN)
@SpringBootTest(classes = <%= mainClass %>.class)
<%_ if (cacheProvider === 'redis') { _%>
@ExtendWith(RedisTestContainerExtension.class)
<%_ } _%>
<%_ if (databaseType === 'neo4j') { _%>
@ExtendWith(AbstractNeo4jIT.class)
<%_ } _%>
class AccountResourceIT <% if (databaseType === 'cassandra') { %>extends AbstractCassandraTest <% } %>{
    static final String TEST_USER_LOGIN = "test";

    @Autowired
    private UserRepository userRepository;
<%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>

    @Autowired
    private AuthorityRepository authorityRepository;
<%_ } _%>

    @Autowired
    private UserService userService;
<%_ if (authenticationType === 'session' && !reactive) { _%>

    @Autowired
    private PersistentTokenRepository persistentTokenRepository;
<%_ } _%>
    <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>

    @Autowired
    private UserSearchRepository mockUserSearchRepository;
    <%_ } _%>

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
<%_ if (!reactive) { _%>
    private MockMvc restAccountMockMvc;
<%_ } else { _%>
    private WebTestClient accountWebTestClient;
<%_ } _%>

    <%_ if (databaseType === 'mongodb' || databaseType === 'neo4j') { _%>
    @BeforeEach
    public void setup() {
        userRepository.deleteAll()<% if (reactive) { %>.block()<% } %>;
    }

    <%_ } _%>
    <%_ if (reactive && testsNeedCsrf) { _%>
    @BeforeEach
    public void setupCsrf() {
        accountWebTestClient = accountWebTestClient.mutateWith(csrf());
    }

    <%_ } _%>
    @Test
    @WithUnauthenticatedMockUser
    <%_ if (!reactive) { _%>
    void testNonAuthenticatedUser() <% if (!reactive) { %>throws Exception <% } %>{
        restAccountMockMvc.perform(get("/api/authenticate")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(""));
    <%_ } else { _%>
    void testNonAuthenticatedUser() {
        accountWebTestClient.get().uri("/api/authenticate")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectBody().isEmpty();
    <%_ } _%>
    }

    @Test
    <%_ if (!reactive) { _%>
    void testAuthenticatedUser() <% if (!reactive) { %>throws Exception <% } %>{
        restAccountMockMvc.perform(get("/api/authenticate")
            .with(request -> {
                request.setRemoteUser(TEST_USER_LOGIN);
                return request;
            })
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().string(TEST_USER_LOGIN));
    <%_ } else { _%>
    void testAuthenticatedUser() {
        accountWebTestClient
            .get().uri("/api/authenticate")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectBody(String.class).isEqualTo(TEST_USER_LOGIN);
    <%_ } _%>
    }

    @Test
    void testGetExistingAccount()<% if (!reactive) { %> throws Exception<% } %> {
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        Set<String> authorities = new HashSet<>();
        authorities.add(AuthoritiesConstants.ADMIN);

        <%= asDto('User') %> user = new <%= asDto('User') %>();
        user.setLogin(TEST_USER_LOGIN);
        user.setFirstName("john");
        user.setLastName("doe");
        user.setEmail("john.doe@jhipster.com");
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        user.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        user.setLangKey("en");
        user.setAuthorities(authorities);
        userService.createUser(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(get("/api/account")
            .accept(MediaType.APPLICATION_JSON))
            .andExpect(status().isOk())
            .andExpect(content().contentType(MediaType.APPLICATION_JSON_VALUE))
            .andExpect(jsonPath("$.login").value(TEST_USER_LOGIN))
            .andExpect(jsonPath("$.firstName").value("john"))
            .andExpect(jsonPath("$.lastName").value("doe"))
            .andExpect(jsonPath("$.email").value("john.doe@jhipster.com"))
            <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
            .andExpect(jsonPath("$.imageUrl").value("http://placehold.it/50x50"))
            <%_ } _%>
            .andExpect(jsonPath("$.langKey").value("en"))
            .andExpect(jsonPath("$.authorities").value(AuthoritiesConstants.ADMIN));
        <%_ } else { _%>
        accountWebTestClient.get().uri("/api/account")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isOk()
            .expectHeader().contentType(MediaType.APPLICATION_JSON_VALUE)
            .expectBody()
            .jsonPath("$.login").isEqualTo(TEST_USER_LOGIN)
            .jsonPath("$.firstName").isEqualTo("john")
            .jsonPath("$.lastName").isEqualTo("doe")
            .jsonPath("$.email").isEqualTo("john.doe@jhipster.com")
            <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
            .jsonPath("$.imageUrl").isEqualTo("http://placehold.it/50x50")
            <%_ } _%>
            .jsonPath("$.langKey").isEqualTo("en")
            .jsonPath("$.authorities").isEqualTo(AuthoritiesConstants.ADMIN);
        <%_ } _%>
    }

    @Test
    <%_ if (!reactive) { _%>
    void testGetUnknownAccount() throws Exception {
        restAccountMockMvc.perform(get("/api/account")
            .accept(MediaType.APPLICATION_PROBLEM_JSON))
            .andExpect(status().isInternalServerError());
    <%_ } else { _%>
    void testGetUnknownAccount() {
        accountWebTestClient.get().uri("/api/account")
            .accept(MediaType.APPLICATION_JSON)
            .exchange()
            .expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    <%_ } _%>
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterValid() throws Exception {
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        ManagedUserVM validUser = new ManagedUserVM();
        validUser.setLogin("test-register-valid");
        validUser.setPassword("password");
        validUser.setFirstName("Alice");
        validUser.setLastName("Test");
        validUser.setEmail("test-register-valid@example.com");
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        validUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        validUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        validUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));
        assertThat(userRepository.findOneByLogin("test-register-valid")<% if (reactive) { %>.blockOptional()<% } %>).isEmpty();

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(validUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(validUser))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        assertThat(userRepository.findOneByLogin("test-register-valid")<% if (reactive) { %>.blockOptional()<% } %>).isPresent();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterInvalidLogin() throws Exception {
        ManagedUserVM invalidUser = new ManagedUserVM();
        invalidUser.setLogin("funky-log(n");// <-- invalid
        invalidUser.setPassword("password");
        invalidUser.setFirstName("Funky");
        invalidUser.setLastName("One");
        invalidUser.setEmail("funky@example.com");
        invalidUser.setActivated(true);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        invalidUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(invalidUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(invalidUser))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> user = userRepository.findOneByEmailIgnoreCase("funky@example.com")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(user).isEmpty();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterInvalidEmail() throws Exception {
        ManagedUserVM invalidUser = new ManagedUserVM();
        invalidUser.setLogin("bob");
        invalidUser.setPassword("password");
        invalidUser.setFirstName("Bob");
        invalidUser.setLastName("Green");
        invalidUser.setEmail("invalid");// <-- invalid
        invalidUser.setActivated(true);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        invalidUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(invalidUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(invalidUser))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> user = userRepository.findOneByLogin("bob")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(user).isEmpty();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterInvalidPassword() throws Exception {
        ManagedUserVM invalidUser = new ManagedUserVM();
        invalidUser.setLogin("bob");
        invalidUser.setPassword("123");// password with only 3 digits
        invalidUser.setFirstName("Bob");
        invalidUser.setLastName("Green");
        invalidUser.setEmail("bob@example.com");
        invalidUser.setActivated(true);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        invalidUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(invalidUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(invalidUser))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> user = userRepository.findOneByLogin("bob")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(user).isEmpty();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterNullPassword() throws Exception {
        ManagedUserVM invalidUser = new ManagedUserVM();
        invalidUser.setLogin("bob");
        invalidUser.setPassword(null);// invalid null password
        invalidUser.setFirstName("Bob");
        invalidUser.setLastName("Green");
        invalidUser.setEmail("bob@example.com");
        invalidUser.setActivated(true);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        invalidUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        invalidUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        invalidUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(invalidUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(invalidUser))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> user = userRepository.findOneByLogin("bob")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(user).isEmpty();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterDuplicateLogin() throws Exception {
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        // First registration
        ManagedUserVM firstUser = new ManagedUserVM();
        firstUser.setLogin("alice");
        firstUser.setPassword("password");
        firstUser.setFirstName("Alice");
        firstUser.setLastName("Something");
        firstUser.setEmail("alice@example.com");
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        firstUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        firstUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        firstUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        // Duplicate login, different email
        ManagedUserVM secondUser = new ManagedUserVM();
        secondUser.setLogin(firstUser.getLogin());
        secondUser.setPassword(firstUser.getPassword());
        secondUser.setFirstName(firstUser.getFirstName());
        secondUser.setLastName(firstUser.getLastName());
        secondUser.setEmail("alice2@example.com");
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        secondUser.setImageUrl(firstUser.getImageUrl());
        <%_ } _%>
        secondUser.setLangKey(firstUser.getLangKey());
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        secondUser.setCreatedBy(firstUser.getCreatedBy());
        secondUser.setCreatedDate(firstUser.getCreatedDate());
        secondUser.setLastModifiedBy(firstUser.getLastModifiedBy());
        secondUser.setLastModifiedDate(firstUser.getLastModifiedDate());
        <%_ } _%>
        secondUser.setAuthorities(new HashSet<>(firstUser.getAuthorities()));

        // First user
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(firstUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(firstUser))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        // Second (non activated) user
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(secondUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(secondUser))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> testUser = userRepository.findOneByEmailIgnoreCase("alice2@example.com")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(testUser).isPresent();
        testUser.get().setActivated(true);
        userRepository.save(testUser.get())<% if (reactive) { %>.block()<% } %>;

        // Second (already activated) user
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(secondUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().is4xxClientError());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(secondUser))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterDuplicateEmail() throws Exception {
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        // First user
        ManagedUserVM firstUser = new ManagedUserVM();
        firstUser.setLogin("test-register-duplicate-email");
        firstUser.setPassword("password");
        firstUser.setFirstName("Alice");
        firstUser.setLastName("Test");
        firstUser.setEmail("test-register-duplicate-email@example.com");
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        firstUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        firstUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        firstUser.setAuthorities(Collections.singleton(AuthoritiesConstants.USER));

        // Register first user
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(firstUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(firstUser))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> testUser1 = userRepository.findOneByLogin("test-register-duplicate-email")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(testUser1).isPresent();

        // Duplicate email, different login
        ManagedUserVM secondUser = new ManagedUserVM();
        secondUser.setLogin("test-register-duplicate-email-2");
        secondUser.setPassword(firstUser.getPassword());
        secondUser.setFirstName(firstUser.getFirstName());
        secondUser.setLastName(firstUser.getLastName());
        secondUser.setEmail(firstUser.getEmail());
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        secondUser.setImageUrl(firstUser.getImageUrl());
        <%_ } _%>
        secondUser.setLangKey(firstUser.getLangKey());
        secondUser.setAuthorities(new HashSet<>(firstUser.getAuthorities()));

        // Register second (non activated) user
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(secondUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(secondUser))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> testUser2 = userRepository.findOneByLogin("test-register-duplicate-email")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(testUser2).isEmpty();

        Optional<<%= asEntity('User') %>> testUser3 = userRepository.findOneByLogin("test-register-duplicate-email-2")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(testUser3).isPresent();

        // Duplicate email - with uppercase email address
        ManagedUserVM userWithUpperCaseEmail = new ManagedUserVM();
        userWithUpperCaseEmail.setId(firstUser.getId());
        userWithUpperCaseEmail.setLogin("test-register-duplicate-email-3");
        userWithUpperCaseEmail.setPassword(firstUser.getPassword());
        userWithUpperCaseEmail.setFirstName(firstUser.getFirstName());
        userWithUpperCaseEmail.setLastName(firstUser.getLastName());
        userWithUpperCaseEmail.setEmail("TEST-register-duplicate-email@example.com");
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        userWithUpperCaseEmail.setImageUrl(firstUser.getImageUrl());
        <%_ } _%>
        userWithUpperCaseEmail.setLangKey(firstUser.getLangKey());
        userWithUpperCaseEmail.setAuthorities(new HashSet<>(firstUser.getAuthorities()));

        // Register third (not activated) user
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(userWithUpperCaseEmail))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(userWithUpperCaseEmail))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> testUser4 = userRepository.findOneByLogin("test-register-duplicate-email-3")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(testUser4).isPresent();
        assertThat(testUser4.get().getEmail()).isEqualTo("test-register-duplicate-email@example.com");

        testUser4.get().setActivated(true);
        userService.updateUser((new <%= asDto('User') %>(testUser4.get())))<% if (reactive) { %>.block()<% } %>;

        // Register 4th (already activated) user
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(secondUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().is4xxClientError());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(secondUser))
            .exchange()
            .expectStatus().is4xxClientError();
        <%_ } _%>
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRegisterAdminIsIgnored() throws Exception {
        ManagedUserVM validUser = new ManagedUserVM();
        validUser.setLogin("badguy");
        validUser.setPassword("password");
        validUser.setFirstName("Bad");
        validUser.setLastName("Guy");
        validUser.setEmail("badguy@example.com");
        validUser.setActivated(true);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        validUser.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        validUser.setLangKey(Constants.DEFAULT_LANGUAGE);
        validUser.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/register")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(validUser))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isCreated());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/register")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(validUser))
            .exchange()
            .expectStatus().isCreated();
        <%_ } _%>

        Optional<<%= asEntity('User') %>> userDup = userRepository.findOne<% if (databaseType === 'sql') { %>WithAuthorities<% } %>ByLogin("badguy")<% if (reactive) { %>.blockOptional()<% } %>;
        assertThat(userDup).isPresent();
        assertThat(userDup.get().getAuthorities()).hasSize(1)
            .containsExactly(<% if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { %>authorityRepository.findById(AuthoritiesConstants.USER).<% if (reactive) { %>block<% } else { %>get<% } %>()<% } %><% if (databaseType === 'cassandra' || databaseType === 'couchbase') { %>AuthoritiesConstants.USER<% } %>);
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testActivateAccount()<% if (!reactive) { %> throws Exception<% } %> {
        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        final String activationKey = "some activation key";
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setLogin("activate-account");
        user.setEmail("activate-account@example.com");
        user.setPassword(RandomStringUtils.random(60));
        user.setActivated(false);
        user.setActivationKey(activationKey);
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>

        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(get("/api/activate?key={activationKey}", activationKey))
            .andExpect(status().isOk());
        <%_ } else { _%>
        accountWebTestClient.get().uri("/api/activate?key={activationKey}", activationKey)
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>

        user = userRepository.findOneByLogin(user.getLogin())<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(user.isActivated()).isTrue();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testActivateAccountWithWrongKey() <% if (!reactive) { %>throws Exception <% } %>{
    <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(get("/api/activate?key=wrongActivationKey"))
            .andExpect(status().isInternalServerError());
    <%_ } else { _%>
        accountWebTestClient.get().uri("/api/activate?key=wrongActivationKey")
            .exchange()
            .expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
    <%_ } _%>
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("save-account")
    void testSaveAccount() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setLogin("save-account");
        user.setEmail("save-account@example.com");
        user.setPassword(RandomStringUtils.random(60));
        user.setActivated(true);
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        <%= asDto('User') %> userDTO = new <%= asDto('User') %>();
        userDTO.setLogin("not-used");
        userDTO.setFirstName("firstname");
        userDTO.setLastName("lastname");
        userDTO.setEmail("save-account@example.com");
        userDTO.setActivated(false);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        userDTO.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
        userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/account")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(userDTO))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(userDTO))
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOne<% if (databaseType === 'sql') { %>WithAuthorities<% } %>ByLogin(user.getLogin())<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(updatedUser.getFirstName()).isEqualTo(userDTO.getFirstName());
        assertThat(updatedUser.getLastName()).isEqualTo(userDTO.getLastName());
        assertThat(updatedUser.getEmail()).isEqualTo(userDTO.getEmail());
        assertThat(updatedUser.getLangKey()).isEqualTo(userDTO.getLangKey());
        assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());<% if (databaseType === 'mongodb' || databaseType === 'couchbase' || databaseType === 'sql' || databaseType === 'neo4j') { %>
        assertThat(updatedUser.getImageUrl()).isEqualTo(userDTO.getImageUrl());<% } %>
        assertThat(updatedUser.isActivated()).isTrue();
        assertThat(updatedUser.getAuthorities()).isEmpty();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("save-invalid-email")
    void testSaveInvalidEmail() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setLogin("save-invalid-email");
        user.setEmail("save-invalid-email@example.com");
        user.setPassword(RandomStringUtils.random(60));
        user.setActivated(true);
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>

        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%= asDto('User') %> userDTO = new <%= asDto('User') %>();
        userDTO.setLogin("not-used");
        userDTO.setFirstName("firstname");
        userDTO.setLastName("lastname");
        userDTO.setEmail("invalid email");
        userDTO.setActivated(false);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        userDTO.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
        userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/account")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(userDTO))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(userDTO))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        assertThat(userRepository.findOneByEmailIgnoreCase("invalid email")<% if (reactive) { %>.blockOptional()<% } %>).isNotPresent();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("save-existing-email")
    void testSaveExistingEmail() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setLogin("save-existing-email");
        user.setEmail("save-existing-email@example.com");
        user.setPassword(RandomStringUtils.random(60));
        user.setActivated(true);
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%= asEntity('User') %> anotherUser = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        anotherUser.setId(UUID.randomUUID().toString());
        <%_ } _%>
        anotherUser.setLogin("save-existing-email2");
        anotherUser.setEmail("save-existing-email2@example.com");
        anotherUser.setPassword(RandomStringUtils.random(60));
        anotherUser.setActivated(true);
        <%_ if (databaseType === 'sql' && reactive) { _%>
        anotherUser.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>

        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(anotherUser)<% if (reactive) { %>.block()<% } %>;

        <%= asDto('User') %> userDTO = new <%= asDto('User') %>();
        userDTO.setLogin("not-used");
        userDTO.setFirstName("firstname");
        userDTO.setLastName("lastname");
        userDTO.setEmail("save-existing-email2@example.com");
        userDTO.setActivated(false);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        userDTO.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
        userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/account")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(userDTO))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(userDTO))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin("save-existing-email")<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(updatedUser.getEmail()).isEqualTo("save-existing-email@example.com");
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("save-existing-email-and-login")
    void testSaveExistingEmailAndLogin() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setLogin("save-existing-email-and-login");
        user.setEmail("save-existing-email-and-login@example.com");
        user.setPassword(RandomStringUtils.random(60));
        user.setActivated(true);
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (searchEngine === 'elasticsearch' && reactive) { _%>
        // Configure the mock search repository
        when(mockUserSearchRepository.save(any()))
            .thenAnswer(invocation -> Mono.just(invocation.getArgument(0)));

        <%_ } _%>
        <%= asDto('User') %> userDTO = new <%= asDto('User') %>();
        userDTO.setLogin("not-used");
        userDTO.setFirstName("firstname");
        userDTO.setLastName("lastname");
        userDTO.setEmail("save-existing-email-and-login@example.com");
        userDTO.setActivated(false);
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        userDTO.setImageUrl("http://placehold.it/50x50");
        <%_ } _%>
        userDTO.setLangKey(Constants.DEFAULT_LANGUAGE);
        userDTO.setAuthorities(Collections.singleton(AuthoritiesConstants.ADMIN));

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/account")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(userDTO))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(userDTO))
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin("save-existing-email-and-login")<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(updatedUser.getEmail()).isEqualTo("save-existing-email-and-login@example.com");
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("change-password-wrong-existing-password")
    void testChangePasswordWrongExistingPassword() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
    <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
    <%_ } _%>
        String currentPassword = RandomStringUtils.random(60);
        user.setPassword(passwordEncoder.encode(currentPassword));
        user.setLogin("change-password-wrong-existing-password");
        user.setEmail("change-password-wrong-existing-password@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(post("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO("1"+currentPassword, "new password")))
            <%_ if (testsNeedCsrf) { _%>
            .with(csrf())<%_ } _%>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO("1"+currentPassword, "new password")))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin("change-password-wrong-existing-password")<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isFalse();
        assertThat(passwordEncoder.matches(currentPassword, updatedUser.getPassword())).isTrue();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("change-password")
    void testChangePassword() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        String currentPassword = RandomStringUtils.random(60);
        user.setPassword(passwordEncoder.encode(currentPassword));
        user.setLogin("change-password");
        user.setEmail("change-password@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(post("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "new password")))
            <%_ if (testsNeedCsrf) { _%>
            .with(csrf())<%_ } _%>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "new password")))
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin("change-password")<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(passwordEncoder.matches("new password", updatedUser.getPassword())).isTrue();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("change-password-too-small")
    void testChangePasswordTooSmall() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        String currentPassword = RandomStringUtils.random(60);
        user.setPassword(passwordEncoder.encode(currentPassword));
        user.setLogin("change-password-too-small");
        user.setEmail("change-password-too-small@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        String newPassword = RandomStringUtils.random(ManagedUserVM.PASSWORD_MIN_LENGTH - 1);

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(post("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, newPassword)))
            <%_ if (testsNeedCsrf) { _%>
            .with(csrf())<%_ } _%>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, newPassword)))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin("change-password-too-small")<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("change-password-too-long")
    void testChangePasswordTooLong() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        String currentPassword = RandomStringUtils.random(60);
        user.setPassword(passwordEncoder.encode(currentPassword));
        user.setLogin("change-password-too-long");
        user.setEmail("change-password-too-long@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        String newPassword = RandomStringUtils.random(ManagedUserVM.PASSWORD_MAX_LENGTH + 1);

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(post("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, newPassword)))
            <%_ if (testsNeedCsrf) { _%>
            .with(csrf())<%_ } _%>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, newPassword)))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin("change-password-too-long")<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("change-password-empty")
    void testChangePasswordEmpty() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        String currentPassword = RandomStringUtils.random(60);
        user.setPassword(passwordEncoder.encode(currentPassword));
        user.setLogin("change-password-empty");
        user.setEmail("change-password-empty@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(post("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .content(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "")))
            <%_ if (testsNeedCsrf) { _%>
            .with(csrf())<%_ } _%>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/change-password")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(new PasswordChangeDTO(currentPassword, "")))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin("change-password-empty")<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(updatedUser.getPassword()).isEqualTo(user.getPassword());
    }
    <%_ if (authenticationType === 'session' && !reactive) { _%>

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("current-sessions")
    void testGetCurrentSessions() <% if (!reactive) { %>throws Exception <% } %>{
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setPassword(RandomStringUtils.random(60));
        user.setLogin("current-sessions");
        user.setEmail("current-sessions@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        PersistentToken token = new PersistentToken();
        token.setSeries("current-sessions");
        <%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>
        token.setUser(user);
        <%_ } else if (databaseType === 'cassandra') { _%>
        token.setUserId(user.getId());
        <%_ } else if (databaseType === 'couchbase') { _%>
        token.setLogin(user.getLogin());
        <%_ } _%>
        token.setTokenValue("current-session-data");
        <%_ if (databaseType === 'cassandra') { _%>
        token.setTokenDate(Instant.parse("2017-03-28T15:25:57.123Z"));
        <%_ } else { _%>
        token.setTokenDate(LocalDate.of(2017, 3, 23));
        <% } %>
        token.setIpAddress("127.0.0.1");
        token.setUserAgent("Test agent");
        persistentTokenRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(token);

        restAccountMockMvc.perform(get("/api/account/sessions"))
            .andExpect(status().isOk())
            .andExpect(jsonPath("$.[*].series").value(hasItem(token.getSeries())))
            .andExpect(jsonPath("$.[*].ipAddress").value(hasItem(token.getIpAddress())))
            .andExpect(jsonPath("$.[*].userAgent").value(hasItem(token.getUserAgent())))
            .andExpect(jsonPath("$.[*].tokenDate").value(hasItem(containsString(<% if (databaseType === 'cassandra') { %>"2017-03-28T15:25:57.123Z"<% } else { %>token.getTokenDate().toString()<% } %>))));
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    @WithMockUser("invalidate-session")
    void testInvalidateSession() <% if (!reactive) { %>throws Exception <% } %>{
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setPassword(RandomStringUtils.random(60));
        user.setLogin("invalidate-session");
        user.setEmail("invalidate-session@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        PersistentToken token = new PersistentToken();
        token.setSeries("invalidate-session");
        <%_ if (databaseType === 'sql' || databaseType === 'mongodb' || databaseType === 'neo4j') { _%>
        token.setUser(user);
        <%_ } else if (databaseType === 'cassandra') { _%>
        token.setUserId(user.getId());
        <%_ } else { _%>
        token.setLogin(user.getLogin());
        <%_ } _%>
        token.setTokenValue("invalidate-data");
        <%_ if (['sql', 'mongodb', 'couchbase', 'neo4j'].includes(databaseType)) { _%>
        token.setTokenDate(LocalDate.of(2017, 3, 23));
        <%_ } else if (databaseType === 'cassandra') { _%>
        token.setTokenDate(Instant.now());
        <%_ } _%>
        token.setIpAddress("127.0.0.1");
        token.setUserAgent("Test agent");
        persistentTokenRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(token);

        assertThat(persistentTokenRepository.findByUser(user)).hasSize(1);

        restAccountMockMvc.perform(delete("/api/account/sessions/invalidate-session")<%_ if (testsNeedCsrf) { _%>.with(csrf())<%_ } _%>)
            .andExpect(status().isOk());

        assertThat(persistentTokenRepository.findByUser(user)).isEmpty();
    }
    <%_ } _%>

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRequestPasswordReset()<% if (!reactive) { %> throws Exception<% } %> {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setPassword(RandomStringUtils.random(60));
        user.setActivated(true);
        user.setLogin("password-reset");
        user.setEmail("password-reset@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(post("/api/account/reset-password/init")
            .content("password-reset@example.com")
            <%_ if (testsNeedCsrf) { _%>
            .with(csrf())<%_ } _%>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/reset-password/init")
            .bodyValue("password-reset@example.com")
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testRequestPasswordResetUpperCaseEmail()<% if (!reactive) { %> throws Exception<% } %> {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setPassword(RandomStringUtils.random(60));
        user.setActivated(true);
        user.setLogin("password-reset-upper-case");
        user.setEmail("password-reset-upper-case@example.com");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(post("/api/account/reset-password/init")
            .content("password-reset-upper-case@EXAMPLE.COM")
            <%_ if (testsNeedCsrf) { _%>
            .with(csrf())<%_ } _%>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/reset-password/init")
            .bodyValue("password-reset-upper-case@EXAMPLE.COM")
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>
    }

    @Test
    <%_ if (!reactive) { _%>
    void testRequestPasswordResetWrongEmail() <% if (!reactive) { %>throws Exception <% } %>{
        restAccountMockMvc.perform(
            post("/api/account/reset-password/init")
                .content("password-reset-wrong-email@example.com")<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isOk());
    <%_ } else { _%>
    void testRequestPasswordResetWrongEmail() {
        accountWebTestClient.post().uri("/api/account/reset-password/init")
            .bodyValue("password-reset-wrong-email@example.com")
            .exchange()
            .expectStatus().isOk();
    <%_ } _%>
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testFinishPasswordReset() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setPassword(RandomStringUtils.random(60));
        user.setLogin("finish-password-reset");
        user.setEmail("finish-password-reset@example.com");
        user.setResetDate(Instant.now().plusSeconds(60));
        user.setResetKey("reset key");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        KeyAndPasswordVM keyAndPassword = new KeyAndPasswordVM();
        keyAndPassword.setKey(user.getResetKey());
        keyAndPassword.setNewPassword("new password");

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/account/reset-password/finish")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(keyAndPassword))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isOk());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/reset-password/finish")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(keyAndPassword))
            .exchange()
            .expectStatus().isOk();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin(user.getLogin())<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(passwordEncoder.matches(keyAndPassword.getNewPassword(), updatedUser.getPassword())).isTrue();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testFinishPasswordResetTooSmall() throws Exception {
        <%= asEntity('User') %> user = new <%= asEntity('User') %>();
        <%_ if (databaseType === 'cassandra') { _%>
        user.setId(UUID.randomUUID().toString());
        <%_ } _%>
        user.setPassword(RandomStringUtils.random(60));
        user.setLogin("finish-password-reset-too-small");
        user.setEmail("finish-password-reset-too-small@example.com");
        user.setResetDate(Instant.now().plusSeconds(60));
        user.setResetKey("reset key too small");
        <%_ if (databaseType === 'sql' && reactive) { _%>
        user.setCreatedBy(Constants.SYSTEM_ACCOUNT);
        <%_ } _%>
        userRepository.save<% if (databaseType === 'sql' && !reactive) { %>AndFlush<% } %>(user)<% if (reactive) { %>.block()<% } %>;

        KeyAndPasswordVM keyAndPassword = new KeyAndPasswordVM();
        keyAndPassword.setKey(user.getResetKey());
        keyAndPassword.setNewPassword("foo");

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/account/reset-password/finish")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(keyAndPassword))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isBadRequest());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/reset-password/finish")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(keyAndPassword))
            .exchange()
            .expectStatus().isBadRequest();
        <%_ } _%>

        <%= asEntity('User') %> updatedUser = userRepository.findOneByLogin(user.getLogin())<%_ if (!reactive) { _%>.orElse(null)<% } else { %>.block()<% } %>;
        assertThat(passwordEncoder.matches(keyAndPassword.getNewPassword(), updatedUser.getPassword())).isFalse();
    }

    @Test<% if (databaseType === 'sql' && !reactive) { %>
    @Transactional<% } %>
    void testFinishPasswordResetWrongKey() throws Exception {
        KeyAndPasswordVM keyAndPassword = new KeyAndPasswordVM();
        keyAndPassword.setKey("wrong reset key");
        keyAndPassword.setNewPassword("new password");

        <%_ if (!reactive) { _%>
        restAccountMockMvc.perform(
            post("/api/account/reset-password/finish")
                .contentType(MediaType.APPLICATION_JSON)
                .content(TestUtil.convertObjectToJsonBytes(keyAndPassword))<% if (testsNeedCsrf) { %>
                .with(csrf())<% } %>)
            .andExpect(status().isInternalServerError());
        <%_ } else { _%>
        accountWebTestClient.post().uri("/api/account/reset-password/finish")
            .contentType(MediaType.APPLICATION_JSON)
            .bodyValue(TestUtil.convertObjectToJsonBytes(keyAndPassword))
            .exchange()
            .expectStatus().isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR);
        <%_ } _%>
    }
}
<%_ } _%>
